﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attack : MonoBehaviour
{
    private GameObject[] m_firePositions;//枪口位置
    private Camera m_mainCamera;//摄像头
    public GameObject m_shellPrefab;//子弹预制体
    private Animator m_animator;
    
    private float shootTime=2;
    private bool shootable = false;//是否允许攻击
    public float shellSpeed = 50;//子弹飞行速度

    void Awake()
    {
        //初始化成员
        m_firePositions = GameObject.FindGameObjectsWithTag("FirePosition");
        m_mainCamera = Camera.main;
        m_animator = GetComponentInChildren<Animator>();
    }

    public void ReloadFirePos()
    {
        m_firePositions = GameObject.FindGameObjectsWithTag("FirePosition");
    }

    public void ReloadAnimator()
    {
        m_animator = GameObject.Find("Body2").GetComponent<Animator>();
        Debug.Log(m_animator);
    }

    void Start()
    {
        SetMouseDisplay(false);
    }

    void Update()
    {
        shootTime += Time.deltaTime;
        if (Input.GetButtonDown("Fire1"))
        {
            
            SetMouseDisplay(false);
            if (shootable && shootTime > 1)//1s射击间隔
            {
                Shoot();
                m_animator.SetBool("Shoot", true);
                shootTime = 0;
            }
        }
        else
        {
            m_animator.SetBool("Shoot", false);
        }
        /* if(Input.GetKeyDown(KeyCode.E))
         {
             if (shootable)
             {
                 BigShoot();
             }
         }*/
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SetMouseDisplay(true);
        }
    }

    void Shoot()
    {
        //获取摄像头中心的世界坐标
        Vector3 cameraCenter = m_mainCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.5f));
        RaycastHit hit;

        //检测射线碰撞
        if (Physics.Raycast(cameraCenter, m_mainCamera.transform.forward, out hit, 50f))
        {
            foreach (GameObject firePos in m_firePositions)
            {
                GameObject go = GameObject.Instantiate(m_shellPrefab);//生产子弹
                go.transform.position = firePos.transform.position;//设置子弹位置
                go.transform.LookAt(hit.point);//让子弹朝向 射线命中的点
                go.GetComponent<Rigidbody>().velocity = shellSpeed * go.transform.forward;//给子弹一个向前的速度
            }

        }
        else
        {
            foreach (GameObject firePos in m_firePositions)
            {
                GameObject go = GameObject.Instantiate(m_shellPrefab, firePos.transform.position, Camera.main.transform.rotation);
                go.GetComponent<Rigidbody>().velocity = shellSpeed * go.transform.forward;
            }
        }
    }

    /* void BigShoot()
     {
          RaycastHit hit;
                 if (Physics.Raycast(m_firePosition.position, Localcamera.transform.forward, out hit, gun.range, Mask))
                 {
                     Debug.Log(hit.collider.name);
                     Debug.DrawLine(m_firePosition.position, hit.point);
                     lineRend.SetPosition(0, m_firePosition.position);
                     lineRend.SetPosition(1, hit.point);
                 }
                 else
                 {
                     Debug.DrawLine(m_firePosition.position, hit.point);
                     Debug.Log("NOT FIND");
                 }
     }*/

    /// <summary>
    /// 隐藏鼠标
    /// </summary>
    /// <param name="isDisplay"></param>
    private void SetMouseDisplay(bool isDisplay)
    {
        if (isDisplay)
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
        else if (!isDisplay)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    public void SetShootable(bool shootable)
    {
        this.shootable = shootable;
    }
}
